package dao

import (
	"gitee.com/hlwqds/go-project/blogService/internal/model"
	"gitee.com/hlwqds/go-project/blogService/pkg/app"
)

func (d *Dao) GetArticle(id uint32) (*model.Article, error) {
	article := model.Article{
		Model: &model.Model{
			ID: id,
		},
	}
	return article.Get(d.engine)
}

func (d *Dao) GetArticleList(title string, state uint8, page, pageSize int) ([]*model.Article, error) {
	article := model.Article{Title: title, State: state}
	pageOffset := app.GetPageOffset(page, pageSize)
	return article.List(d.engine, pageOffset, pageSize)
}

func (d *Dao) CreateArticle(name string, state uint8, createdBy, desc, content, imageUrl string) error {
	article := model.Article{
		Title:         name,
		Desc:          desc,
		Content:       content,
		CoverImageUrl: imageUrl,
		State:         state,
		Model: &model.Model{
			CreatedBy: createdBy,
		},
	}
	return article.Create(d.engine)
}
func (d *Dao) UpdateArticle(
	id uint32,
	name string,
	state uint8,
	modifiedBy,
	desc,
	content,
	imageUrl string) error {

	article := model.Article{
		Model: &model.Model{
			ID: id,
		},
	}

	values := map[string]interface{}{
		"state":           state,
		"desc":            desc,
		"content":         content,
		"cover_image_url": imageUrl,
		"modified_by":     modifiedBy,
	}
	if name != "" {
		values["name"] = name
	}
	return article.Update(d.engine, values)
}

func (d *Dao) DeleteArticle(id uint32) error {
	article := model.Article{
		Model: &model.Model{
			ID: id,
		},
	}
	return article.Delete(d.engine)
}
